package com.sui.bigdata.rtcadmin.repository.mapper;

import com.sui.bigdata.rtcadmin.repository.model.JobConfig;
import com.sui.bigdata.rtcadmin.repository.model.JobStatusInfo;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author YongChen
 * @date 2020/3/9 14:04
 * @description
 * @email yong_chen@sui.com
 */
@Mapper
@Component
public interface JobStatusMapper {

    @Select("<script>"
            + "select * from flink_sql_job_status_info where 1=1 "
            + "<if test='jobName != null'>"
            + "and job_name = #{jobName} "
            + "</if>"
            + "<if test='appId != null'>"
            + "and app_id = #{appId} "
            + "</if>"
            + "<if test='engine != null'>"
            + "and engine = #{engine} "
            + "</if>"
            + "</script>")
    @ResultType(JobStatusInfo.class)
    List<JobStatusInfo> queryAll(@Param("jobName") String jobName, @Param("appId") String appId, @Param("engine") String engine);


    @Insert("insert into flink_sql_job_status_info(job_name,developer,app_id,job_id,status,engine)\n" +
            "VALUES (#{jobName},#{developer},#{appId},#{jobId},#{status},#{engine})\n" +
            "ON DUPLICATE KEY UPDATE " +
            "job_name=#{jobName},developer=#{developer},app_id=#{appId},job_id=#{jobId},status=#{status},engine=#{engine}")
    void updateSubmitedJob(@Param("jobName") String jobName,
                                 @Param("developer") String developer,
                                 @Param("appId") String appId,
                                 @Param("jobId") String jobId,
                                 @Param("status") String status,
                                 @Param("engine") String engine);

    @Update("UPDATE flink_sql_job_status_info SET `status`=#{status} WHERE job_name = #{jobName} " +
            " and app_id = #{appId}")
    void updateStatus(@Param("jobName") String jobName,
                      @Param("appId") String appId,
                      @Param("status") String status);
}
